package com.acegear.horizon.domain.events.handlers;

import com.acegear.horizon.domain.events.UserEquipCreateEvent;
import com.acegear.horizon.domain.events.UserEquipDeleteEvent;
import com.acegear.horizon.domain.events.UserEquipModifiedEvent;
import com.acegear.horizon.domain.models.jpa.UserEquip;
import com.acegear.horizon.domain.repository.jpa.UserEquipRepository;
import com.acegear.horizon.vendor.ons.ONSConsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;

/**
 * Created by wangsike on 2017/4/6.
 */
@Component
public class EquipEventHandler {

    @Autowired
    private UserEquipRepository userEquipRepository;
    @Autowired
    private ONSConsumer consumer;

    @PostConstruct
    public void equipCreatedONS() {
        consumer.consume(UserEquipCreateEvent.class, this::equipCreated);
    }

    @PostConstruct
    public void userEquipModifiedONS() {
        consumer.consume(UserEquipModifiedEvent.class, this::userEquipModified);
    }

    @PostConstruct
    public void userEquipDeleteONS() {
        consumer.consume(UserEquipDeleteEvent.class, this::userEquipDelete);
    }

    @Transactional
    public void equipCreated(UserEquipCreateEvent userEquipCreateEvent) {
        userEquipRepository.save(new UserEquip(userEquipCreateEvent.getUserEquipId(), userEquipCreateEvent.getUserId()));
    }

    public void userEquipModified(UserEquipModifiedEvent userEquipModifiedEvent) {

    }

    @Transactional
    public void userEquipDelete(UserEquipDeleteEvent userEquipDeleteEvent) {
        userEquipRepository.delete(userEquipDeleteEvent.getUserEquipId());
    }
}
